import math

from InsertSort import InsertSort


def bucket_sort(array,buckesize=10):
    # 确定最大值和最小值
    min, max = array[0],array[0]
    for i in array:
        if i < min:
            min = i
        if i > max:
            max = i
    # 确定桶的数量
    bucket_number = (max-min)//buckesize + 1
    # 初始化桶
    bucket_list =  list([] for _ in range(bucket_number))
    # 把数据放入相应的桶
    for a in array:
        bucket_index = (a-min)//buckesize
        bucket_list[bucket_index].append(a)
    # 对每个桶进行排序,这里使用插入排序
    for i in range(bucket_number):
        print('bucket_',i,'is ',InsertSort(bucket_list[i]).sort())
    # 合并数据
    result = []
    for j in bucket_list:
        if len(j) != 0:
            result.extend(j)
    return result

array = [55,43,32,55,34,30,23,44]

result=bucket_sort(array)

print(result)

